Read first N lines of a fileΒΆ
Read first N lines of a file.
def file_read_from_head(fname, N):
from itertools import islice
with open(fname) as f:
for line in islice(f, N):
print(line)
# test
file_read_from_head('test.txt', 2)
Output:
Welcome to w3resource.com.
Append this text.Append this text.Append this text.